﻿<%@ WebHandler Language="C#" Class="ShowCommunityLogs" %>

using System;
using System.Web;
using System.Text;
using System.Data;
using Chenxin.Lib.Utility;
using Chenxin.Lib.House;
using Chenxin.Lib.House.BLL;

public class ShowCommunityLogs : AjaxHandlerBase
{
    /// <summary>
    /// 基本权限项
    /// </summary>
    public ShowCommunityLogs()
    {
        //导出
        htTypePermit.Add("export", Constants.Permit_CommunityLogs_Export);  
    }

    override public void RequestProcess(HttpContext context)
    {
        string strOp = context.Request["op"];
        string strResponse = "";
        switch (strOp.ToLower())
        {
            //查询
            case "search":
                strResponse = Search(context);
                break;
            default:
                break;
        }
        context.Response.Write(strResponse);
    }
    
    //*******************************************
    //功能概要       ：获得非诉一览
    //参数説明       ：context 请求的上下文
    //返回值説明     ：无
    //功能詳細       ：获得非诉一览
    //作成者         ：zou.xiaochun
    //作成日         ：2010-10-08
    //*******************************************
    private string Search(HttpContext context)
    {
        try
        {
            //页码
            int strPageIndex = (string.IsNullOrEmpty(context.Request["pageindex"])) ? 1 : int.Parse(context.Request["pageindex"]) + 1;
            //页显示行数
            int strPageSize = (string.IsNullOrEmpty(context.Request["pagesize"])) ? 30 : int.Parse(context.Request["pagesize"]);
            //查询条件
            string sqlWhere = GenerateCondition(context);

            //查询分页信息
            Chenxin.Lib.House.BLL.House_CommunityLogs bllLogs = new House_CommunityLogs();
            System.Data.DataSet ds = bllLogs.GetModelListByCoditions(sqlWhere, strPageIndex, strPageSize);
            //总页数
            int count = (int)ds.Tables[1].Rows[0][0];

            return string.Format("{{success:'1',msg:'{0}',data:{{count:'{1}',{2}}}}}", "成功", count, JsonHelp.Convert2Json(ds.Tables[0]));
        }
        catch (Exception ex)
        {
            return string.Format("{{success:'0',msg:'{0}：{1}',data:''}}", "意外错误", ex.Message);
        }
    }

    //*******************************************
    //功能概要       ：查询权限
    //参数説明       ：context 请求上下文
    //返回值説明     ：程序处理信息提示
    //功能詳細       ：生成查询条件语句
    //作成者         ：chen.yugong
    //作成日         ：2010-06-29
    //修改者         ：王玮
    //修改日         ：2010-07-13
    //*******************************************
    public string GenerateCondition(HttpContext context)
    {
        try
        {
            //获取页面查询条件
            Guid city = (string.IsNullOrEmpty(context.Request["cityid"])) ? Guid.Empty : new Guid(context.Request["cityid"]);
            Guid company = (string.IsNullOrEmpty(context.Request["companyid"])) ? Guid.Empty : new Guid(context.Request["companyid"]);
            Guid businessdistrict = (string.IsNullOrEmpty(context.Request["areaid"])) ? Guid.Empty : new Guid(context.Request["areaid"]);
            Guid departs = (string.IsNullOrEmpty(context.Request["departid"])) ? Guid.Empty : new Guid(context.Request["departid"]);
            Guid employee = (string.IsNullOrEmpty(context.Request["userid"])) ? Guid.Empty : new Guid(context.Request["userid"]);

            string starttime = context.Request.QueryString["DateFr"];
            string endtime = context.Request.QueryString["DateTo"];
            string status = context.Request.QueryString["OprType"];
            string opcontext = context.Request.QueryString["OperationContext"];

            System.Text.StringBuilder sb = new System.Text.StringBuilder();

            //判断级联菜单
            if (employee != Guid.Empty)
                sb.AppendFormat(" and (UserId = '{0}') ", employee);
            else if (departs != Guid.Empty)
                sb.AppendFormat(" and (DepartId = '{0}')", departs);
            else if (businessdistrict != Guid.Empty)
            {
                Chenxin.Lib.Hr.Entity.Hr_DepartmentInfo item = null;
                string strCachkey = string.Format(Chenxin.Lib.Cache.CacheDataType.GLOBAL_DEPARTMENT, businessdistrict);
                Chenxin.Lib.Cache.CacheService cs = new Chenxin.Lib.Cache.CacheService(Chenxin.Lib.Cache.CacheConn.xCacheConn.GLOBAL);
                item = cs.GetObject<Chenxin.Lib.Hr.Entity.Hr_DepartmentInfo>(strCachkey);
                if (!cs.Hit)
                {
                    item = Chenxin.Lib.Hr.DAL.Hr_DepartmentInfo.GetModel(businessdistrict);
                    if (item != null) cs.Set(strCachkey, item, DateTime.Now.AddDays(1));
                }

                if (item == null) return string.Format("{{success:'0',msg:'{0}',data:''}}", "获取商圈或分类数据失败");

                sb.AppendFormat(" and (DepartId in (select departId from v_Hr_DepartmentInfo where DepartSrc like '{0}%'))", item.DepartSrc);
            }
            else if (company != Guid.Empty)
            {
                sb.AppendFormat(" and CompanyId = '{0}'", company);
            }
            else if (city != Guid.Empty)
            {
                sb.AppendFormat(" and CityId = '{0}'", city);
            }
            else
            {
                sb.AppendFormat(" and CompanyId = '{0}'", CurrentUserInfo.CompanyId);
            }
            
            //查询权限判断权限
            System.Text.StringBuilder sb1 = new System.Text.StringBuilder();
            if (CurrentUserInfo.ContainsPermit(Constants.Permit_CommunityLogs_All))
            {
                sb1.Append(" (1=1) ");
            }
            else if (CurrentUserInfo.ContainsPermit(Constants.Permit_CommunityLogs_City))
            {
                sb1.AppendFormat(" (CityId = '{0}')", CurrentUserInfo.CityId);
            }
            else if (CurrentUserInfo.ContainsPermit(Constants.Permit_CommunityLogs_Company))
            {
                sb1.AppendFormat(" (CompanyId = '{0}')", CurrentUserInfo.CompanyId);
            }
            else if (CurrentUserInfo.ContainsPermit(Constants.Permit_CommunityLogs_Depart))
            {
                sb1.Append(" (EXISTS(");
                sb1.AppendFormat("Select 1 from Chenxin_Business.dbo.t_Business_EmpSetting M WHERE House_CommunityLogs.DepartId=M.DepartId And M.Userid= '{0}' ", CurrentUserInfo.UserId.ToString());
                sb1.Append(")) ");
            }
            else
            {
                sb1.AppendFormat(" (UserId = '{0}')", CurrentUserInfo.UserId);
            }

            if (!String.IsNullOrEmpty(sb.ToString()))
                sb1.Append(sb.ToString());

            if (!String.IsNullOrEmpty(starttime))
            {
                sb1.Append(string.Format(" and convert(varchar(10),(convert(datetime,Addtime,120)),120)  >= '{0}'", (starttime)));
            }
            if (!String.IsNullOrEmpty(endtime))
            {
                sb1.Append(string.Format(" and convert(varchar(10),(convert(datetime,Addtime,120)),120)  <= '{0}'", (endtime)));
            }
            if (!String.IsNullOrEmpty(status))
            {
                sb1.AppendFormat(" And OprType like '%{0}%'", status);
            }
            if (!String.IsNullOrEmpty(opcontext))
            {
                sb1.AppendFormat(" And OperationContext like '%{0}%'", opcontext);
            }
            return sb1.ToString();
        }
        catch (Exception ex)
        {
            return string.Format("{{success:'0',msg:'{0}：{1}',data:''}}", "意外错误", ex.Message);
        }
    }
}